package com.bdqn.controller;

import com.bdqn.dao.UpRepositoryMapper;
import com.bdqn.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class UpRepositoryController {

    @Resource
    UpRepositoryMapper up;

    @RequestMapping("/deleteSale")
    @ResponseBody
    @CrossOrigin
    public int deleteSale(int saleid){
        return up.deleteSale(saleid);
    }

    @RequestMapping("/getsale")
    @ResponseBody
    @CrossOrigin
    public Object getSale(HttpServletResponse response, @RequestBody GetSalePojo pojo) throws ParseException {
        response.setHeader("Access-Control-Allow-Origin","*");
        Object [] obj=null;
        String sql = "phone_sale ";
        if(pojo.getNewDate().equals("")){
            sql += "where saleData BETWEEN '2000-1-1' and  now() ";
        }else {
            sql += "where saleData BETWEEN '"+pojo.getSaleDate()+"' and '"+pojo.getNewDate()+"'";
        }
        if(pojo.getPermissions().equals("0")){ //普通员工只能看到自己的销售记录
            sql += "and uid="+pojo.getUid();
        }
        if (pojo.getPermissions()!="0"&&pojo.getNewUid()!=""&&pojo.getNewUid()!=null){
            sql += "and uid="+pojo.getNewUid();
        }
        obj = up.getAllsale(sql,pojo.getIndex());
        List<Phone_SalePojo> list = (List<Phone_SalePojo>) obj[0];
        int count = ((ArrayList<Integer>)obj[1]).get(0);
        PagePojo page = new PagePojo(pojo.getIndex(),count,count%10==0?count/10:count/10+1,list);
        return page;
    }

    @RequestMapping("/getTypePone")//根据品牌获取型号
    @ResponseBody
    public List<SpMangePojo> getName(HttpServletResponse response, String bid){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<SpMangePojo> list = up.getName(bid);
        return list;
    }
    @RequestMapping("/getAllBrand")//根据品牌获取型号
    @ResponseBody
    public List<String> getBrand(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<String> list = up.getBrand();
        return list;
    }
    @RequestMapping("/getNo0List")
    @ResponseBody
    public List<PhoneKcPojo> getNo0List(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<PhoneKcPojo> list = up.getNo0List();
        return list;
    }
    @RequestMapping("/saleone")//录入销售订单
    @ResponseBody
    public  int saleone(HttpServletResponse response,String id,String userid,String num) throws ParseException {
        response.setHeader("Access-Control-Allow-Origin","*");
        //获取指定id的手机
        PhoneKcPojo pojo = up.isIDgetPhone(id);
        //修改手机数量
        int phonenum = pojo.num-Integer.parseInt(num);
        int updatenum = up.updateNum(Integer.toString(phonenum),id);
        int insert = up.insertSale(/*time,*/userid,id,num);
        if (insert == 1){
            return 1;
        }else{
            return 0;
        }
    }



}
